<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use app\index\model\Personals;
use app\index\model\Users;
/*
个人控制器
 */
class Personal extends Common
{  
    //个人中心
    public function center(){
        $uid = input('uid');//获取用户id
        if (!empty($uid)) {
            $result=Personals::centerSel($uid);
            if ($result) {
                $return = ['state' => '200', 'msg' => '查询成功', 'result' =>$result];
            } else {
                $return = ['state' => '201', 'msg' => '用户不存在!'];
            }
        }else{
            $return = ['state' => '202', 'msg' => '请求参数不足！'];
        }

        return json($this->array_nonull($return));
    }

	 //个人资料展示
     public function personaldata()
    {
        $uid = input('uid');//获取用户id
        if (!empty($uid)) {
        	$result=Personals::Mydetails($uid);
            if ($result) {
                $return = ['state' => '200', 'msg' => '查询成功', 'result' =>$result];
            } else {
                $return = ['state' => '201', 'msg' => '用户不存在!'];
            }
        }else{
            $return = ['state' => '202', 'msg' => '请求参数不足！'];
        }

        return json($this->array_nonull($return));
    }
	 //个人资料编辑接口
   public function updateDatum(){
        $uid = (int)input('uid','');//用户id
        if(!empty($uid)){
         $rs=Users::selOne($uid);
          if($rs){
                    $data=input('post.');//接受所有的值
                    // file_put_contents('filename1111.txt', json_encode($data));die;
                    $re=Personals::Myset($uid,$data);
                    if($re){
                        return json(['state'=>'200','msg'=>'修改成功']);
                    }else{
                        return json(['state'=>'201','msg'=>'修改失败再试试!']);
                    }      
            }else{
                return json(['state'=>'202','msg'=>'用户不存在!']);
            }

        }else{
            return json(['state'=>'203','msg'=>'请求参数不足！']);
        }

    }
    //忘记密码接口
    public function phone_change(){
       $uid = Request::instance()->post('uid','');//用户id
      if(empty($uid)){
          return json(['state'=>'203','msg'=>'用户为空']);
        }
        $userPhone = input('userPhone','');//用户手机号
        $verify = input('verify','');//验证码
        $loginPwd = input('loginPwd','');//设置的密码
        $rs=Users::selOne($uid);
       if($rs){
          $loginSecret=$rs['loginSecret'];//获取验证码
           $time1= $rs['update_time'];//获取时间
           //得出过期时间
            $time = time()-strtotime($time1);
            //过期时间
             $n=intval($time/60);
          if($verify==$loginSecret && $n<10){
             $data=['loginPwd'=>md5($loginPwd)];
         $result=Personals::forgetSet($uid,$data);
         if($result){
             return json($this->array_nonull(['state'=>'200','msg'=>'设置成功','result'=>$result]));
         }else{
            return json(['state'=>'201','msg'=>'设置失败再试试']);  
         }
       }else{
         return json(['state'=>'204','msg'=>'验证码不正确']);
       }
         
       }else{
         return json(['state'=>'202','msg'=>'用户已不存在']);
       }  
    }
    /*
     * @获取验证码
     * @函数 get_password_verify
     * @功能 找回密码的验证码
     * @参数 [mobile:手机号]
     */
    public function get_password_verify()
    {
        $userPhone = input('userPhone');
        if(empty($userPhone)){
            return json(['state'=>'204','msg'=>"手机号为空"]);
        }
         if(mb_strlen($userPhone)!='11'){
            return json(['state'=>'205','msg'=>'手机号格式不正确']);
        }else{
            $re=Db::name('user_info')->where(['userPhone'=>$userPhone])->field('userPhone,nickname')->find();
            if($re){ 
              $code = mt_rand(111111,999999);
              $data=['loginSecret'=>$code,'update_time'=>date("Y-m-d H:i:s")];
             $rs=Db::name('user_info')->where(['userPhone'=>$userPhone])->update($data);
             if($rs){
               $arrOne=Db::name('user_info')->where(['userPhone'=>$userPhone])->find();
               $num1=$arrOne['loginSecret'];
             header('content-type:text/html;charset=utf-8');
             $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
           
            $smsConf = array(
            'key'   => '14ed239162b132b8009fe878b529951a', //您申请的APPKEY
            'mobile'    => "$userPhone", //接受短信的用户手机号码
            'tpl_id'    => '78391', //您申请的短信模板ID，根据实际情况修改
            'tpl_value' =>'#code#='.$num1 //您设置的模板变量，根据实际情况修改
            );
            $content = $this->juhecurl($sendUrl,$smsConf,1); //请求发送短信
            if($content){
            $result = json_decode($content,true);
            $error_code = $result['error_code'];
            if($error_code == 0){
            //状态为0，说明短信发送成功
            // echo "短信发送成功,短信ID：".$result['result']['sid'];
                return json(['state'=>'200','msg'=>"短信发送成功"]);
            }else{
            //状态非0，说明失败
            $msg = $result['reason'];
            // echo "短信发送失败(".$error_code.")：".$msg;
            return json(['state'=>'201','msg'=>"短信发送失败(".$error_code.")：".$msg]);
            }
            }else{
            //返回内容异常，以下可根据业务逻辑自行修改
            // echo "请求发送短信失败";
            return json(['state'=>'202','msg'=>"请求发送短信失败"]);
            }   
           }else{
             return json(['state'=>'203','msg'=>"存储验证码失败"]);
           }   
         }else{
            return json(['state'=>'206','msg'=>"你的手机号未注册"]);
         }   
            
        } 
        
    }
    //修改手机号
    public function PhoneSet()
    {
        $uid=(int)input('uid');//用户id
        $userPhone=input('userPhone');//用户手机号
        $verify=input('verify');//验证码
        if(empty($uid) || empty($userPhone) || empty($verify)){
             return json(['state'=>'203','msg'=>"参数缺失"]);
        }
        $rs=Users::selOne($uid);
        if($rs){
           $mobile=Db::name('mobile')->where(['userPhone'=>$userPhone])->find();
            $time1= $mobile['time'];//获取时间
            $time = time()-strtotime($time1);
            //过期时间
             $n=intval($time/60);
            if($mobile['num']==$verify && $n<10){ 
                $data=['userPhone'=>$userPhone];
               $rd=Db::name('user_info')
              ->where(['uid'=>$uid,'userFlag'=>'1'])
              ->update($data);
              if($rd){
                 return json(['state'=>'200','msg'=>"修改成功"]); 
              }else{
                return json(['state'=>'201','msg'=>"你的手机号没变动"]); 
              }
            }else{
                  return json(['state'=>'204','msg'=>"验证码有误"]);  
            }
           
        }else{
            return json(['state'=>'202','msg'=>'用户已不存在']);
        }
    }
    //修改密码
    public function amendPwd(){
        $uid=(int)input('uid');//用户id
        $loginPwd=input('loginPwd');//原密码
        $newPwd=input('newPwd');//新密码
        if(empty($uid) || empty($loginPwd) || empty($newPwd)){
             return json(['state'=>'203','msg'=>"参数缺失"]);
        }
       // $rs=Users::selOne($uid);
        $rs=Db::name('user_info')
              ->where(['uid'=>$uid,'userFlag'=>'1'])
              ->field('uid,loginPwd')
              ->find();
        if($rs){
            if($rs['loginPwd']==md5($loginPwd)){
                $data=['loginPwd'=>md5($newPwd)];
               $rd=Db::name('user_info')
              ->where(['uid'=>$uid,'userFlag'=>'1'])
              ->update($data);
              if($rd){
                 return json(['state'=>'200','msg'=>"修改密码成功"]); 
              }else{
                return json(['state'=>'201','msg'=>"新密码没有改动过"]); 
              }
            }else{
                  return json(['state'=>'204','msg'=>"原密码输入有误"]);  
            }
           
        }else{
            return json(['state'=>'202','msg'=>'用户已不存在']);
        }
    }
}